package com.lebang.service.cences;

import com.lebang.component.RedisClient;
import com.lebang.domain.CencesBrandActive;
import com.lebang.example.CencesBrandActiveExample;
import com.lebang.mapper.CencesBrandActiveMapper;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.model.MyConfig;
import com.lebang.model.cences.CencesMsg;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.lebang.service.buss.MemberService.MEMBER_LOGIN_RECORD;

/**
 * 新手任务
 *
 * @Author xwj
 * @Date 2020-04-09
 */
@Service
public class CencesBrandActiveService extends BaseService<CencesBrandActive, Integer, CencesBrandActiveExample> {
    @Autowired
    private CencesBrandActiveMapper baseMapper;

    @Autowired
    private LbbMemberMapper lbbMemberMapper;

    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    @Autowired
    private RedisClient redisClient;


    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 统计指定日期的渠道效果
     *
     * @param end   结束日期
     * @param brand 渠道
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));
        //所有的渠道来源
        if (brand == null) {
            brand = lbbMemberMapper.selectBrand();
        }

        CencesBrandActive vo = new CencesBrandActive(time, 0, MyConfig.getEnv());
        brand.forEach(s -> {
            //统计渠道活跃人数
            activeNum(s, time, vo);
        });

        baseMapper.insertBrandActive(vo);


        cencesMsgMongodb.save(new CencesMsg("渠道活跃人数统计", (System.currentTimeMillis() - starTime)));

    }

    /**
     * 统计渠道活跃人数
     *
     * @param appStore 渠道
     * @param time     时间
     * @param vo       渠道活跃人数记录
     */
    private void activeNum(String appStore, int time, CencesBrandActive vo) {
        switch (appStore) {
            case "vivo":
                vo.setVivo(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "oppo":
                vo.setOppo(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "huawei":
                vo.setHuawei(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "xiaomi":
                vo.setXiaomi(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "meizu":
                vo.setMeizu(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "360":
                vo.setSll(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "yingyongbao":
                vo.setYyb(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            case "xiaozhuozhangshuang":
                vo.setXiaozhuo(redisClient.hlen(MEMBER_LOGIN_RECORD + time + ":" + appStore).intValue());
                break;
            default:
                break;
        }
    }

    public void deleteByTime(Integer time) {
        CencesBrandActiveExample cencesBrandActiveExample = new CencesBrandActiveExample();
        cencesBrandActiveExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        baseMapper.deleteByExample(cencesBrandActiveExample);
    }
}
